package com.gxa.admin.recycling.controller;

import com.gxa.admin.recycling.service.GoodsOrderService;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.util.StringUtil;
import com.gxa.core.recycling.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * 周志远
 */
@CrossOrigin
@RestController
@RequestMapping("/goodsOrder")
@Api(tags = "商品兑换管理页面接口")
@Transactional(rollbackFor = {Throwable.class})
public class GoodsOrderController {
    @Autowired
    GoodsOrderService goodsOrderService;
    @PostMapping("/findByPage")
    @ApiOperation("查询所有商品兑换+分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "兑换单id",required = false,dataType = "Long"),
            @ApiImplicitParam(name = "provinces",value = "省市区",required = false,dataType = "String"),
            @ApiImplicitParam(name = "comityName",value = "小区名称",required = false,dataType = "String"),
            @ApiImplicitParam(name = "userName",value = "兑换人姓名",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state",value = "订单状态",required = false,dataType = "String"),
            @ApiImplicitParam(name = "exchangetime",value = "兑换时间",required = false,dataType = "String"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "每页显示数",required = true,dataType = "Integer")
    })
    public Map findByPage(Long id, String provinces, String comityName, String userName, String state, String exchangetime, Integer page, Integer size){
        List<Map> allByPage = goodsOrderService.findAllByPage(id, provinces, comityName, userName, state, exchangetime, page, size);
        List<Map> allByPage1 = goodsOrderService.findAllByPage(id, provinces, comityName, userName, state, exchangetime, 1, 10000);
        int size1 = allByPage1.size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(allByPage);
        jsonResult.put("count",size1);
        return jsonResult.getDataMap();
    }
    @ApiOperation("查询单个商品兑换")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "兑换单id",required = false,dataType = "Long")
    })
    @PostMapping("/orderDetail")
    public Map GoodsOrderDetail(Long id){
        Map byId = goodsOrderService.findById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(byId);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getCount")
    @ApiOperation("商品兑换量统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "小区名称",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "cycle",value = "统计周期",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "from",value = "自定义事件起止日期",required = false,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "to",value = "自定义事件截止日期",required = false,dataType = "String",paramType = "query")
    })
    public Map getAll(String name,String cycle,String from,String to){
        String end = null;
        String start = null;
        if (!StringUtil.isEmpty(cycle)){
            end = new Timestamp(System.currentTimeMillis()).toString();
            start = TimeUtil.rollBackTime(cycle);
        }
        List<Map<String, Object>> allCount = goodsOrderService.getAllCount(name, start, end, from, to);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("200");
        jsonResult.setData(allCount);
        return jsonResult.getDataMap();
    }
}
